//
//  Task.swift
//  Aria2Proxy
//
//  Created by 张宇飞 on 2024/11/5.
//

import CryptoKit
import Foundation

private func MD5(string: String) -> String {
    let digest = Insecure.MD5.hash(data: Data(string.utf8))

    return digest.map {
        String(format: "%02hhx", $0)
    }.joined()
}

enum Aria2TaskState {
    case paused
    case running
    case finished
}

struct Aria2Task {
    let id: String
    let hls: HLS
    let saveDir: String

    init(hls: HLS, saveDir: String) {
        self.hls = hls
        self.saveDir =
            URL(string: "/Users")!.appending(
                components: NSUserName(), "Downloads", saveDir
            ).absoluteString.removingPercentEncoding!
        // 生成 id
        self.id = "Aria2Proxy:\(MD5(string: hls.url + saveDir))"
    }
}
